package com.gmail.viktor.yuryev;

import com.intellij.openapi.components.*;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Viktor Yuryev on 4/10/2014.
 */

@State(
        name = "MyRemoteServers",
        storages = {
                @Storage(
                        file = StoragePathMacros.APP_CONFIG + "/other.xml"
                )}
)

public class MyRemoteServers implements PersistentStateComponent<Element> {
    private List<ServerDetails> servers;


    private MyRemoteServers(){
        servers = new ArrayList<ServerDetails>();
    }

    public static MyRemoteServers getInstance() {
        MyRemoteServers service = ServiceManager.getService(MyRemoteServers.class);
        return service;
    }


    public void load() {
        ServerDetails e = new ServerDetails("csapp013co.netcracker.com", "yuryev", "Shket1111", 22);

        e.putFolders("C:\\SVN\\Boxster\\tools_common\\site", "/u02/netcracker/toms/u27_c13_6830/applications/NetCracker/NetCrackerWebApp");
        e.putFolders("c:\\TEMP\\test_classes\\scripts\\com\\", "/netcracker/config/u27_c13_6830/applications/NetCracker/APP-INF/temp");
        e.setServerHome("/netcracker/config/u27_c13_6830");
        e.setSudoPrefix("sudo -u netcrk ");
        servers.add(e);
    }

    public List<ServerDetails> getServerList() {
        return new ArrayList<ServerDetails>(servers);
    }

    public void addServer(ServerDetails connectionData) {
        servers.add(connectionData);
    }

    @Override
    public Element getState() {
        return XmlSerializer.serialize(servers.toArray(new ServerDetails[servers.size()]));
    }

    @Override
    public void loadState(Element state) {
        servers.clear();
        servers.addAll(Arrays.asList(XmlSerializer.deserialize(state, ServerDetails.class)));
        load();
    }


}
